package com.belly.office;


import twitter4j.Twitter;
import twitter4j.auth.RequestToken;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuInflater;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class MainScreenActivity extends BaseActivity {

	Constants getFinals;
	Facebook facebook;
	AsyncFacebookRunner mAsyncRunner;

	private int TWITTER_AUTH;
	private Twitter mTwitter;
	private RequestToken mRequestToken;

	private SharedPreferences mPrefs;
	private String accessToken;
	private String accessTokenSecret;
	private double x;
	public String fbToken;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.officemainlayout);
		getFinals = new Constants();
		fbToken= getFinals.fbAppid;
		facebook = new Facebook(fbToken);
		mAsyncRunner = new AsyncFacebookRunner(facebook);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		facebook.extendAccessTokenIfNeeded(this, null);
	}

	public void fbConnect(View v) {
		if (isNetworkAvailable(MainScreenActivity.this)) {
			/*
			 * Get existing access_token if any
			 */
			mPrefs = getPreferences(MODE_PRIVATE);
			String access_token = mPrefs.getString("access_token", null);
			long expires = mPrefs.getLong("access_expires", 0);
			if (access_token != null) {
				facebook.setAccessToken(access_token);
			}
			if (expires != 0) {
				facebook.setAccessExpires(expires);
			}

			/*
			 * Only call authorize if the access_token has expired.
			 */
			if (!facebook.isSessionValid()) {

				facebook.authorize(this, new String[] { "user_photos" },
						new DialogListener() {
							@Override
							public void onComplete(Bundle values) {
								SharedPreferences.Editor editor = mPrefs.edit();
								editor.putString("access_token",
										facebook.getAccessToken());
								editor.putLong("access_expires",
										facebook.getAccessExpires());
								editor.commit();
							}

							@Override
							public void onFacebookError(FacebookError error) {
							}

							@Override
							public void onError(DialogError e) {
							}

							@Override
							public void onCancel() {
							}
						});
			} else
				Toast.makeText(this, "Already Connected", Toast.LENGTH_LONG)
						.show();
		} else
			Toast.makeText(this, "No Internet Connection", Toast.LENGTH_LONG)
					.show();
	}

	public void twitConnect(View v) {
		Toast.makeText(this, "done not implemented here", Toast.LENGTH_LONG)
				.show();
	};

	public void gplusConnect(View v) {
		Toast.makeText(this, "not in Motion yet", Toast.LENGTH_LONG).show();
	}

	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	    	MenuInflater inflato = getSupportMenuInflater();
	        inflato.inflate(R.menu.activity_splash, menu);
	        
	        return true;
	    }
	    
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	      switch (item.getItemId()) {
	      case R.id.menuitem1:
	        Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
	            .show();
	        break;
	      case R.id.menuitem2:
	        Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
	            .show();
	        break;

	      default:
	        break;
	      }

	      return true;
	    }
}
